#!/bin/bash
#
# 重启 Varnish 和 Hitch 服务
# 作者: 知汛项目组
# 最后更新: 2024-11-07

# 颜色定义
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'

# 默认配置
RESTART_VARNISH=true
RESTART_HITCH=true
SHOW_LOGS=false
HARD_RESTART=false

# 显示帮助信息
show_help() {
    cat << EOF
用法: $0 [选项]

重启 Varnish 和 Hitch 服务

选项:
    --varnish-only    仅重启Varnish
    --hitch-only      仅重启Hitch
    --hard            强制重启（先停止再启动）
    --logs            重启后显示日志
    --help            显示此帮助信息

示例:
    # 重启所有服务
    sudo bash $0

    # 仅重启Hitch并查看日志
    sudo bash $0 --hitch-only --logs

    # 强制重启所有服务
    sudo bash $0 --hard
EOF
}

# 解析参数
while [[ $# -gt 0 ]]; do
    case $1 in
        --varnish-only)
            RESTART_HITCH=false
            shift
            ;;
        --hitch-only)
            RESTART_VARNISH=false
            shift
            ;;
        --hard)
            HARD_RESTART=true
            shift
            ;;
        --logs)
            SHOW_LOGS=true
            shift
            ;;
        --help)
            show_help
            exit 0
            ;;
        *)
            echo -e "${RED}未知参数: $1${NC}"
            show_help
            exit 1
            ;;
    esac
done

# 检查root权限
if [ "$EUID" -ne 0 ]; then
    echo -e "${RED}请使用root权限运行此脚本: sudo $0${NC}"
    exit 1
fi

echo "======================================"
echo "重启 Varnish/Hitch 服务"
echo "======================================"

# 重启Varnish
if [ "$RESTART_VARNISH" = true ]; then
    echo -e "${YELLOW}[1/2] 重启 Varnish 服务...${NC}"
    
    if [ "$HARD_RESTART" = true ]; then
        systemctl stop varnish
        sleep 2
        systemctl start varnish
    else
        systemctl restart varnish
    fi
    
    if systemctl is-active --quiet varnish; then
        echo -e "${GREEN}✓ Varnish 服务重启成功${NC}"
        netstat -tlnp | grep 6081 | head -1
    else
        echo -e "${RED}✗ Varnish 服务重启失败${NC}"
        exit 1
    fi
fi

# 重启Hitch
if [ "$RESTART_HITCH" = true ]; then
    echo -e "${YELLOW}[2/2] 重启 Hitch 服务...${NC}"
    
    # 尝试不同的服务名称
    if systemctl list-units --full -all | grep -q "hitch-cdn.service"; then
        SERVICE_NAME="hitch-cdn"
    elif systemctl list-units --full -all | grep -q "hitch.service"; then
        SERVICE_NAME="hitch"
    else
        echo -e "${RED}✗ 找不到 Hitch 服务${NC}"
        exit 1
    fi
    
    if [ "$HARD_RESTART" = true ]; then
        systemctl stop $SERVICE_NAME
        sleep 2
        systemctl start $SERVICE_NAME
    else
        systemctl restart $SERVICE_NAME
    fi
    
    if systemctl is-active --quiet $SERVICE_NAME; then
        echo -e "${GREEN}✓ Hitch 服务重启成功${NC}"
        netstat -tlnp | grep 6443 | head -1
    else
        echo -e "${RED}✗ Hitch 服务重启失败${NC}"
        exit 1
    fi
fi

# 显示日志
if [ "$SHOW_LOGS" = true ]; then
    echo ""
    echo "======================================"
    echo "服务日志"
    echo "======================================"
    
    if [ "$RESTART_VARNISH" = true ]; then
        echo -e "${YELLOW}Varnish 日志:${NC}"
        journalctl -u varnish -n 10 --no-pager
        echo ""
    fi
    
    if [ "$RESTART_HITCH" = true ]; then
        echo -e "${YELLOW}Hitch 日志:${NC}"
        journalctl -u $SERVICE_NAME -n 10 --no-pager
    fi
fi

echo ""
echo -e "${GREEN}✓ 操作完成${NC}"

